package com.sdy.dcsb.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.model.*;
import com.sdy.dcsb.biz.service.*;
import com.sdy.dcsb.web.controller.inboundservice.util.HttpReqUtil;
import com.sdy.dcsb.web.util.SearchBrowserKeywordsUtil;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * 标准服务配置表 前端控制器
 * </p>
 *
 * @author fb
 * @since 2019-09-25
 */
@Slf4j
@Controller
@RequestMapping("/EsbInboundMain")
@Api(tags = "标准服务相关接口")
public class EsbInboundMainController extends BaseController {
    @Autowired
    private EsbInboundMainService esbInboundMainService;
    @Autowired
    private EsbServiceTreeService esbServiceTreeService;
    @Autowired
    private EsbInboundHttpService esbInboundHttpService;
    @Autowired
    private EsbInboundH5Service esbInboundH5Service;
    @Autowired
    private EsbInboundWebserviceService esbInboundWebserviceService;
    @Autowired
    private EsbAtomServiceService esbAtomServiceService;
    @Autowired
    private EsbAdapderHttpService esbAdapderHttpService;
    @Autowired
    private EsbAdapderSoapService esbAdapderSoapService;
    @Autowired
    private UserOperationLogService userOperationLogService;
    @Value("${server.port}")
    private String serverPort;

    /**
     * 查询树目录下的标准服务
     * @param id 主键,当前节点id
     * @param current ,当前页
     * @param size ,每页数量
     * @param type 查询类别,1:服务名称 2:服务编码 其他:查全部
     * @param message ,查询值
     * @return
     */
    @GetMapping("/selectSubServiceById")
    @ResponseBody
    @ApiOperation("查询树目录下的标准服务，分页，可按编码或名称模糊查询,id必传")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "当前节点id"),
            @ApiImplicitParam(dataType = "Integer", name = "current", value = "当前页"),
            @ApiImplicitParam(dataType = "Integer", name = "size", value = "每页数量"),
            @ApiImplicitParam(dataType = "Integer", name = "type", value = "查询类别,1:服务名称 2:服务编码 其他:查全部"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "查询值"),
    })
    public Response selectSubServiceById(Integer id, Integer current, Integer size, Integer type, String message) {
        if(isEmpty(id)) {
            return Response.error("当前节点id不能为空");
        }
        JSONObject object = new JSONObject();
        if(isEmpty(current)) {
            current = 1;
        }
        if(isEmpty(size)) {
            size = 10;
        }
        object.put("size",size);
        object.put("current",current);
        object.put("searchCount",true);

        Integer total = 0;
        Integer page = 0;

        if(!isEmpty(message)) {
            try {
                message = URLDecoder.decode(message,"utf-8");
            }catch(IOException e) {

            }
            if(isEmpty(type)) {
                return Response.error("查询类别不能为空");
            }
            if(type.equals(1)) {
                List<EsbInboundMainJoint> resultCount = esbInboundMainService.findCountServiceByIdName(id, message);
                total = resultCount.size();
                if(total%size==0){
                    page = total/size;
                }else {
                    page = total/size+1;
                }
                List<EsbInboundMainJoint> result = esbInboundMainService.findSubServiceByIdName(id, current, size, message);
                object.put("records",result);
                object.put("total",total);
                object.put("pages",page);
            }else if(type.equals(2)) {
                List<EsbInboundMainJoint> resultCount = esbInboundMainService.findCountServiceByIdCode(id, message);
                total = resultCount.size();
                if(total%size==0){
                    page = total/size;
                }else {
                    page = total/size+1;
                }
                List<EsbInboundMainJoint> result = esbInboundMainService.findSubServiceByIdCode(id, current, size, message);
                object.put("records",result);
                object.put("total",total);
                object.put("pages",page);
            }
        }else {
            List<EsbInboundMainJoint> resultCount = esbInboundMainService.findCountServiceById(id);
            total = resultCount.size();
            if(total%size==0){
                page = total/size;
            }else {
                page = total/size+1;
            }
            List<EsbInboundMainJoint> result = esbInboundMainService.findSubServiceById(id, current, size);
            object.put("records",result);
            object.put("total",total);
            object.put("pages",page);
        }
        return Response.success(object);
    }

    /**
     * 分页数据
     * @param type 查询类别,1:服务名称 2:服务编码 其他:查全部
     * @param message ,查询值
     * @param page [current, size]
     * @return
     */
    @GetMapping("/pageData")
    @ResponseBody
    @ApiOperation("查询标准服务的所有分页数据，可按名称或编码模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "type", value = "查询类别,1:服务名称 2:服务编码 其他:查全部"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "查询值"),
    })
    public Response pageData(Integer type, String message, Page<EsbInboundMain> page) {
        LambdaQueryWrapper<EsbInboundMain> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(EsbInboundMain::getCreateTime);
        if(!isEmpty(message)) {
            try {
                message = URLDecoder.decode(message,"utf-8");
            }catch(IOException e) {
                log.error(e.getMessage());
            }
            if(type.equals(1)) {
                wrapper.like(EsbInboundMain::getServname,message);
            }else if(type.equals(2)) {
                wrapper.like(EsbInboundMain::getServcode,message);
            }
        }
        IPage<EsbInboundMain> result = esbInboundMainService.page(page, wrapper);
        return Response.success(result);
    }

    /**
     * 查询单条数据/通过服务id或者原子服务编码
     * @param serviceid ,当前标准服务serviceid
     * @param atomcode ,原子服务编码
     * @return
     */
    @GetMapping("/select")
    @ResponseBody
    @ApiOperation("查询单条标准服务数据/通过服务id或者原子服务编码")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前标准服务serviceid"),
            @ApiImplicitParam(dataType = "String", name = "atomcode", value = "原子服务编码"),
    })
    public Response select(Integer serviceid, String atomcode) {
        if(isEmpty(serviceid) && !isEmpty(atomcode)) {
            try {
                atomcode = URLDecoder.decode(atomcode,"utf-8");
            }catch(IOException e) {

            }
            QueryWrapper<EsbInboundMain> wrapper = new QueryWrapper<>();
            wrapper.eq("atomcode",atomcode);
            return Response.success(esbInboundMainService.list(wrapper));
        }else if(isEmpty(atomcode) && !isEmpty(serviceid)) {
            return Response.success(esbInboundMainService.getById(serviceid));
        }else if(!isEmpty(atomcode) && !isEmpty(serviceid)) {
            return Response.error("请选择1个请求参数查询");
        }
        return Response.error("请求参数不能为空");
    }

    /**
     * 修改单条服务信息
     * @param serviceid ,当前服务serviceid
     * @param servname ,服务名称
     * @param servversion ,服务版本
     * @param status ,工作状态,1:工作态,0:注销态
     * @param overTime ,超时时间/ms（最小30000）
     * @return
     */
    @GetMapping("/update")
    @ResponseBody
    @ApiOperation("修改单条服务信息，服务编码和原子服务编码不可修改")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前服务serviceid"),
            @ApiImplicitParam(dataType = "String", name = "servname", value = "服务名称"),
            @ApiImplicitParam(dataType = "String", name = "servversion", value = "服务版本"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "工作状态,1:工作态,0:注销态"),
            @ApiImplicitParam(dataType = "String", name = "overTime", value = "超时时间/ms（最小30000）"),
    })
    public Response update(HttpServletRequest request, Integer serviceid, String servname, String servversion, String status, String overTime) {
        if(!isEmpty(servname) || !isEmpty(servversion) || !isEmpty(status) || !isEmpty(overTime)){
            try {
                servname = URLDecoder.decode(servname,"utf-8");
                servversion = URLDecoder.decode(servversion,"utf-8");
                status = URLDecoder.decode(status,"utf-8");
                overTime = URLDecoder.decode(overTime,"utf-8");
            }catch(IOException e) {

            }
            QueryWrapper<EsbInboundMain> wrapper = new QueryWrapper<>();
            wrapper.eq("serviceid", serviceid);
            List<EsbInboundMain> result = esbInboundMainService.list(wrapper);
            EsbInboundMain esbInboundMain = new EsbInboundMain();
            EsbServiceTree esbServiceTree = new EsbServiceTree();
            Integer i = 0;
            Integer j = 0;
            if(!result.get(0).getServname().equals(servname)) {
                esbInboundMain.setServname(servname);
                i++;
                esbServiceTree.setName(servname);
                j++;
            }
            if(!result.get(0).getServversion().equals(servversion)){
                esbInboundMain.setServversion(servversion);
                if(!isEmpty(servversion)) {
                    i++;
                }
            }
            if(!result.get(0).getStatus().equals(status)){
                esbInboundMain.setStatus(status);
                if(!isEmpty(status)) {
                    i++;
                }
            }
            if(!result.get(0).getOverTime().equals(overTime)){
                esbInboundMain.setOverTime(overTime);
                if(!isEmpty(overTime)) {
                    i++;
                }
            }
            if(i >= 1) {
                esbInboundMain.setUpdateTime(new Date());
                userOperationLogService.savaUserLog(request, "修改服务信息：" + result.get(0).getServcode());
                boolean type = esbInboundMainService.update(esbInboundMain, new UpdateWrapper<EsbInboundMain>().eq("serviceid", serviceid));
                if(type) {
                    if(j >= 1){
                        userOperationLogService.savaUserLog(request, "修改服务树目录：" + result.get(0).getServname() +
                                "--->" + servname);
                        esbServiceTreeService.update(esbServiceTree, new UpdateWrapper<EsbServiceTree>().eq("code",serviceid));
                    }
                }
                return Response.success("信息修改成功");
            }else {
                return Response.error("信息无变化");
            }
        }else {
            return Response.error("修改参数不能为空");
        }
    }

    /**
     * 删除数据
     * @param serviceid ,当前服务serviceid
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation("删除单条服务数据")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前服务serviceid"),
    })
    public Response delete(HttpServletRequest request, Integer serviceid) {
        if(!isEmpty(serviceid)) {
            String name = esbInboundMainService.getById(serviceid).getServname();
            String code = esbInboundMainService.getById(serviceid).getServcode();
            userOperationLogService.savaUserLog(request, "删除服务信息：" + code);
            if(esbInboundMainService.removeById(serviceid)) {
                QueryWrapper<EsbServiceTree> wrapper = new QueryWrapper<>();
                wrapper.eq("code", serviceid);
                userOperationLogService.savaUserLog(request, "删除服务树目录：" + name);
                if(esbServiceTreeService.remove(wrapper)) {
                    return Response.success("删除服务成功");
                }else {
                    return Response.error("删除树目录失败!");
                }
            }else {
                return Response.error("删除服务失败!");
            }
        }else {
            return Response.error("请求serviceid不能为空");
        }
    }

    /**
     * 查询服务接入信息
     * @param serviceid ,当前服务serviceid
     * @return
     */
    @GetMapping("/selectRequestMethod")
    @ResponseBody
    @ApiOperation("查询服务接入信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前服务serviceid"),
    })
    public Response selectRequestMethod(Integer serviceid) {
        if(!isEmpty(serviceid)) {
            EsbInboundHttp esbInboundHttp = esbInboundHttpService.getById(serviceid);
            EsbInboundWebservice esbInboundWebservice = esbInboundWebserviceService.getById(serviceid);
            EsbInboundH5 esbInboundH5 = esbInboundH5Service.getById(serviceid);
            if(!isEmpty(esbInboundHttp)) {
                JSONObject object = new JSONObject();
                object.put("type","1");
                object.put("datas",esbInboundHttp);
                return Response.success(object);
            }
            if(!isEmpty(esbInboundWebservice)) {
                JSONObject object = new JSONObject();
                object.put("type","2");
                object.put("datas",esbInboundWebservice);
                return Response.success(object);
            }
            if(!isEmpty(esbInboundH5)) {
                JSONObject object = new JSONObject();
                object.put("type","3");
                object.put("datas",esbInboundH5);
                return Response.success(object);
            }
            return Response.success();
        }else {
            return Response.error("请求serviceid不能为空");
        }
    }

    /**
     * 新增或修改服务请求类型方式
     * @param serviceid ,当前服务serviceid
     * @param type ,请求类型,1:http 2:webservice 3:h5
     * @param httpType ,http请求类型,1:get 2:post
     * @param endpoint ,webservice:wsdl地址
     * @param servicename ,webservice:service名
     * @param portname ,webservice:port名
     * @param operationname ,webservice:方法名
     * @param targetaddress ,h5:目标地址
     * @param projectpath ,h5:工程路径
     * @param tarType ,h5:返回类型,H:HTML页面 P：图片
     * @return
     */
    @GetMapping("/insertRequestMethod")
    @ResponseBody
    @ApiOperation("新增或修改服务请求类型方式")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前服务serviceid"),
            @ApiImplicitParam(dataType = "String", name = "type", value = "请求类型,1:http 2:webservice 3:h5"),
            @ApiImplicitParam(dataType = "Integer", name = "httpType", value = "http请求类型,1:get 2:post"),
            @ApiImplicitParam(dataType = "String", name = "endpoint", value = "webservice:wsdl地址"),
            @ApiImplicitParam(dataType = "String", name = "servicename", value = "webservice:service名"),
            @ApiImplicitParam(dataType = "String", name = "portname", value = "webservice:port名"),
            @ApiImplicitParam(dataType = "String", name = "operationname", value = "webservice:方法名"),
            @ApiImplicitParam(dataType = "String", name = "targetaddress", value = "h5:目标地址"),
            @ApiImplicitParam(dataType = "String", name = "projectpath", value = "h5:工程路径"),
            @ApiImplicitParam(dataType = "String", name = "tarType", value = "h5:返回类型,H:HTML页面 P：图片"),
    })
    public Response insertRequestMethod(HttpServletRequest request, Integer serviceid, String type,
                                        Integer httpType,
                                        String endpoint, String servicename, String portname, String operationname,
                                        String targetaddress, String projectpath, String tarType) {
        if(isEmpty(serviceid) || isEmpty(type)) {
            return Response.error("请求serviceid或请求类型不能为空");
        }
        try {
            if(!isEmpty(endpoint)) {
                endpoint = URLDecoder.decode(endpoint,"utf-8");
            }
            if(!isEmpty(servicename)) {
                servicename = URLDecoder.decode(servicename,"utf-8");
            }
            if(!isEmpty(portname)) {
                portname = URLDecoder.decode(portname,"utf-8");
            }
            if(!isEmpty(operationname)) {
                operationname = URLDecoder.decode(operationname,"utf-8");
            }
            if(!isEmpty(targetaddress)) {
                targetaddress = URLDecoder.decode(targetaddress,"utf-8");
            }
            if(!isEmpty(projectpath)) {
                projectpath = URLDecoder.decode(projectpath,"utf-8");
            }
            if(!isEmpty(tarType)) {
                tarType = URLDecoder.decode(tarType,"utf-8");
            }
        }catch(IOException e) {

        }
        QueryWrapper<EsbInboundHttp> esbInboundHttpWrapper = new QueryWrapper<>();
        esbInboundHttpWrapper.eq("serviceid",serviceid);
        List<EsbInboundHttp> esbInboundHttpResult = esbInboundHttpService.list(esbInboundHttpWrapper);
        QueryWrapper<EsbInboundWebservice> esbInboundWebserviceWrapper = new QueryWrapper<>();
        esbInboundWebserviceWrapper.eq("serviceid",serviceid);
        List<EsbInboundWebservice> esbInboundWebserviceResult = esbInboundWebserviceService.list(esbInboundWebserviceWrapper);
        QueryWrapper<EsbInboundH5> esbInboundH5Wrapper = new QueryWrapper<>();
        esbInboundH5Wrapper.eq("serviceid",serviceid);
        List<EsbInboundH5> esbInboundH5Result = esbInboundH5Service.list(esbInboundH5Wrapper);
        Integer count = 0;
        if(esbInboundHttpResult.size() != 0 || esbInboundWebserviceResult.size() != 0 || esbInboundH5Result.size() != 0) {
            count = 1;
        }
        if(type.equals("1")) {
            if(esbInboundHttpResult.size() == 0) {
                if(count == 1 ) {
                    return Response.error("已存在一种接入类型，不能再新增");
                }else {
                    EsbInboundHttp esbInboundHttp = new EsbInboundHttp();
                    esbInboundHttp.setServiceid(serviceid);
                    esbInboundHttp.setHttptype(httpType == null ? 1:httpType);
                    esbInboundHttpService.save(esbInboundHttp);
                    userOperationLogService.savaUserLog(request, "新增服务http接入信息：" +
                            esbInboundMainService.getById(serviceid).getServcode());
                    return Response.success("新增成功");
                }
            }else {
                if(Objects.equals(esbInboundHttpResult.get(0).getHttptype(), httpType)) {
                    return Response.error("信息无变化");
                }
                EsbInboundHttp esbInboundHttp = new EsbInboundHttp();
                QueryWrapper<EsbInboundHttp> wrapperNew = new QueryWrapper<>();
                esbInboundHttp.setHttptype(httpType);
                userOperationLogService.savaUserLog(request, "修改服务http接入信息：" +
                        esbInboundMainService.getById(serviceid).getServcode());
                esbInboundHttpService.update(esbInboundHttp, wrapperNew.eq("serviceid",serviceid));
                return Response.success("修改成功");
            }
        }else if(type.equals("2")) {
            if(esbInboundWebserviceResult.size() == 0) {
                if(count == 1 ) {
                    return Response.error("已存在一种接入类型，不能再新增");
                }else {
                    EsbInboundWebservice esbInboundWebservice = new EsbInboundWebservice();
                    esbInboundWebservice.setServiceid(serviceid);
                    esbInboundWebservice.setEndpoint(endpoint == null ? "":endpoint);
                    esbInboundWebservice.setServicename(servicename == null ? "":servicename);
                    esbInboundWebservice.setPortname(portname == null ? "":portname);
                    esbInboundWebservice.setOperationname(operationname == null ? "":operationname);
                    esbInboundWebserviceService.save(esbInboundWebservice);
                    userOperationLogService.savaUserLog(request, "新增服务webservice接入信息：" +
                            esbInboundMainService.getById(serviceid).getServcode());
                    return Response.success("新增成功");
                }
            }else {
                if(Objects.equals(esbInboundWebserviceResult.get(0).getEndpoint(), endpoint) && Objects.equals(
                        esbInboundWebserviceResult.get(0).getServicename(), servicename) && Objects.equals(
                        esbInboundWebserviceResult.get(0).getPortname(), portname) && Objects.equals(
                        esbInboundWebserviceResult.get(0).getOperationname(), operationname)) {
                    return Response.error("信息无变化");
                }
                EsbInboundWebservice esbInboundWebservice = new EsbInboundWebservice();
                QueryWrapper<EsbInboundWebservice> wrapperNew = new QueryWrapper<>();
                esbInboundWebservice.setEndpoint(endpoint == null ? "":endpoint);
                esbInboundWebservice.setServicename(servicename == null ? "":servicename);
                esbInboundWebservice.setPortname(portname == null ? "":portname);
                esbInboundWebservice.setOperationname(operationname == null ? "":operationname);
                userOperationLogService.savaUserLog(request, "修改服务webservice接入信息：" +
                        esbInboundMainService.getById(serviceid).getServcode());
                esbInboundWebserviceService.update(esbInboundWebservice,wrapperNew.eq("serviceid",serviceid));
                return Response.success("修改成功");
            }
        }else if(type.equals("3")) {
            if(esbInboundH5Result.size() == 0) {
                if(count == 1 ) {
                    return Response.error("已存在一种接入类型，不能再新增");
                }else {
                    EsbInboundH5 esbInboundH5 = new EsbInboundH5();
                    esbInboundH5.setServiceid(serviceid);
                    esbInboundH5.setTargetaddress(targetaddress == null ? "":targetaddress);
                    esbInboundH5.setProjectpath(projectpath == null ? "":projectpath);
                    esbInboundH5.setTarType(tarType == null ? "":tarType);
                    esbInboundH5Service.save(esbInboundH5);
                    userOperationLogService.savaUserLog(request, "新增服务H5接入信息：" +
                            esbInboundMainService.getById(serviceid).getServcode());
                    return Response.success("新增成功");
                }
            }else {
                if(Objects.equals(esbInboundH5Result.get(0).getTargetaddress(), targetaddress) && Objects.equals(
                        esbInboundH5Result.get(0).getProjectpath(), projectpath) && Objects.equals(
                        esbInboundH5Result.get(0).getTarType(), tarType)) {
                    return Response.error("信息无变化");
                }
                EsbInboundH5 esbInboundH5 = new EsbInboundH5();
                QueryWrapper<EsbInboundH5> wrapperNew = new QueryWrapper<>();
                esbInboundH5.setTargetaddress(targetaddress == null ? "":targetaddress);
                esbInboundH5.setProjectpath(projectpath == null ? "":projectpath);
                esbInboundH5.setTarType(tarType == null ? "":tarType);
                esbInboundH5Service.update(esbInboundH5,wrapperNew.eq("serviceid",serviceid));
                userOperationLogService.savaUserLog(request, "修改服务H5接入信息：" +
                        esbInboundMainService.getById(serviceid).getServcode());
                return Response.success("修改成功");
            }
        }else {
            return Response.error("请输入已有的请求类型");
        }
    }

    /**
     * 查询服务清单
     * @param type 查询类别,1:服务名称 2:服务编码 其他:查全部
     * @param message ,查询值
     * @param status ,查询状态,全部:2 在用:1 不在用:0
     * @param current ,当前页
     * @param size ,每页数量
     * @return
     */
    @GetMapping("/selectServiceList")
    @ResponseBody
    @ApiOperation("查询服务清单，分页，可按编码或名称模糊查询和服务状态查询")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "type", value = "查询类别,1:服务名称 2:服务编码 其他:查全部"),
            @ApiImplicitParam(dataType = "String", name = "message", value = "查询值"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "查询状态,全部:2 在用:1 不在用:0"),
            @ApiImplicitParam(dataType = "Integer", name = "current", value = "当前页"),
            @ApiImplicitParam(dataType = "Integer", name = "size", value = "每页数量"),
    })
    public Response selectServiceList(String type, String message, String status, Integer current, Integer size) {
        try {
            if(!isEmpty(message)) {
                message = URLDecoder.decode(message,"utf-8");
            }
        }catch(IOException e) {

        }
        if("2".equals(status)) {
            status = "0,1";
        }
        JSONObject object = new JSONObject();
        if(isEmpty(current)) {
            current = 1;
        }
        if(isEmpty(size)) {
            size = 10;
        }
        object.put("size",size);
        object.put("current",current);
        object.put("searchCount",true);

        Integer total = 0;
        Integer page = 0;

        if(isEmpty(message) || (!isEmpty(message) && !isEmpty(type) && "1".equals(type))) {
            List<EsbInboundMain> resultCount = esbInboundMainService.findServiceListCount("servname", message, status);
            total = resultCount.size();
            if(total%size==0){
                page = total/size;
            }else {
                page = total/size+1;
            }
            List<EsbInboundMain> result = esbInboundMainService.findServiceList("servname", message, status, current, size);
            object.put("records",result);
            object.put("total",total);
            object.put("pages",page);
        }else {
            if(isEmpty(type)) {
                return Response.error("查询类别不能为空");
            }
            if(type.equals("2")) {
                List<EsbInboundMain> resultCount = esbInboundMainService.findServiceListCount("servcode", message, status);
                total = resultCount.size();
                if(total%size==0){
                    page = total/size;
                }else {
                    page = total/size+1;
                }
                List<EsbInboundMain> result = esbInboundMainService.findServiceList("servcode", message, status, current, size);
                object.put("records",result);
                object.put("total",total);
                object.put("pages",page);
            }else {
                return Response.error("请输入指定的查询类别");
            }
        }
        return Response.success(object);
    }

    /**
     * 服务模拟，测试application/json型接口调用
     * @param map json形式
     * @return
     */
    @PostMapping("/serviceSimulationJson")
    @ResponseBody
    @ApiOperation("服务模拟，测试application/json型接口调用")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Map<String,Object>", name = "map", value = "{\n" +
                    "\"servcode\": \"33.1111.zjhz.smzx.ycsl.patchApply.SynReq\",\n" +
                    "\"data\": {\n" +
                    "\"id_nbr\": \"330721\",\n" +
                    "\"name\": \"fubin\"\n" +
                    "}\n" +
                    "}"),
    })
    public Response serviceSimulationJson(@RequestBody Map<String,Object> map) {
        String servcode = (String) map.get("servcode");
        if(isEmpty(map.get("data"))) {
            return Response.error("请求的json缺少data");
        }
        JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(map.get("data")));
        return Response.success(object);
    }

    /**
     * 查询所有标准服务，不分页
     * @return
     */
    @GetMapping("/selectAllService")
    @ResponseBody
    @ApiOperation("查询所有标准服务，不分页")
    @ApiImplicitParams({
    })
    public Response selectAllService() {
        QueryWrapper<EsbInboundMain> esbInboundMainWrapper = new QueryWrapper<>();
        esbInboundMainWrapper.orderByDesc("create_time");
        List<EsbInboundMain> esbInboundMainResult = esbInboundMainService.list(esbInboundMainWrapper);
        return Response.success(esbInboundMainResult);
    }

    /**
     * 通过服务编码查询服务
     * @param servcode ,服务编码
     * @return
     */
    @GetMapping("/selectServiceByServcode")
    @ResponseBody
    @ApiOperation("通过服务编码查询服务")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "servcode", value = "服务编码"),
    })
    public Response selectServiceByServcode(String servcode) {
        QueryWrapper<EsbInboundMain> esbInboundMainWrapper = new QueryWrapper<>();
        esbInboundMainWrapper.like("servcode", servcode);
        List<EsbInboundMain> esbInboundMainResult = esbInboundMainService.list(esbInboundMainWrapper);
        return Response.success(esbInboundMainResult);
    }

    /**
     * 通过服务编码查询对应原子服务的请求头类型
     * @param servcode ,服务编码
     * @return
     */
    @GetMapping("/selectServiceContentByServcode")
    @ResponseBody
    @ApiOperation("通过服务编码查询对应原子服务的请求头类型")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "servcode", value = "服务编码"),
    })
    public Response selectServiceContentByServcode(String servcode) {
        if(isEmpty(servcode)) {
            return Response.error("请求的服务编码不能为空");
        }
        QueryWrapper<EsbInboundMain> esbInboundMainWrapper = new QueryWrapper<>();
        esbInboundMainWrapper.eq("servcode", servcode);
        List<EsbInboundMain> esbInboundMainResult = esbInboundMainService.list(esbInboundMainWrapper);
        if(esbInboundMainResult.size() == 0) {
            return Response.error("请求数据查询不到！");
        }
        String atomCode = esbInboundMainResult.get(0).getAtomcode();
        List<EsbAtomservice> esbAtomserviceResult = esbAtomServiceService.selectAtomServiceByCode(atomCode);
        return Response.success(esbAtomserviceResult);
    }

    /**
     * 服务模拟
     * @param map json形式
     * @return
     */
    @PostMapping("/serviceSimulation")
    @ResponseBody
    @ApiOperation("服务模拟")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Map<String,Object>", name = "map", value = "{\n" +
                    "\"servcode\": \"33.1111.zjhz.dcsbbzfwxxcx.SynReq\",\n" +
                    "\"data\": {\n" +
                    "\"servcode\": \"33.1111.zjhz.smzx.sjzyjauthenticatepdf.SynReq\",\n" +
                    "\"name\": \"fubin\"\n" +
                    "}\n" +
                    "}"),
    })
    public Response serviceSimulation(HttpServletRequest request, @RequestBody Map<String,Object> map) {
        String servcode = "";
        try{
            // 获取服务编码
            servcode = (String) map.get("servcode");
            try{
                userOperationLogService.savaUserLog(request, "服务模拟：" + servcode);
            }catch (Exception e) {

            }
        }catch (Exception e) {
            return Response.error("map中未传入servcode字段");
        }
        JSONObject constants = new JSONObject();
        JSONObject object = null;
        try{
            if(isEmpty(map.get("data"))) {
                map.put("data",constants);
            }
            object = JSONObject.parseObject(JSONObject.toJSONString(map.get("data")));
        }catch (Exception e) {
            return Response.error("请传入json数据");
        }
        JSONObject headerObject = null;
        try{
            if(isEmpty(map.get("header"))) {
                map.put("header",constants);
            }
            headerObject = JSONObject.parseObject(JSONObject.toJSONString(map.get("header")));
        }catch (Exception e) {
            return Response.error("请传入json数据");
        }
        HashMap<String, String> headerMap = new HashMap<String, String>();
        for (Map.Entry<String, Object> entry : headerObject.entrySet()) {
            headerMap.put(entry.getKey(), String.valueOf(headerObject.get(entry.getKey())));
        }

        QueryWrapper<EsbInboundMain> esbInboundMainWrapper = new QueryWrapper<>();
        esbInboundMainWrapper.eq("servcode", servcode);
        List<EsbInboundMain> esbInboundMainResult = esbInboundMainService.list(esbInboundMainWrapper);
        if(esbInboundMainResult.size() == 0) {
            return Response.error("请求的接口的服务编码查询不到！");
        }
        Integer serviceId = esbInboundMainResult.get(0).getServiceid();
        String version = esbInboundMainResult.get(0).getServversion();
        EsbInboundHttp esbInboundHttpResult = esbInboundHttpService.getById(serviceId);
        EsbInboundWebservice esbInboundWebserviceResult = esbInboundWebserviceService.getById(serviceId);
        EsbInboundH5 esbInboundH5Result = esbInboundH5Service.getById(serviceId);
        // 决定接口类型
        String type = null;
        if(!isEmpty(esbInboundHttpResult)) {
            type = "servlets/";
        }else if(!isEmpty(esbInboundWebserviceResult)){
            type = "services/";
            return Response.error("webservice接口暂不支持服务模拟");
        }else if(!isEmpty(esbInboundH5Result)) {
            type = "h5servlets/";
            return Response.error("h5服务暂不支持服务模拟");
        }else {
            return Response.error("接口无对应的请求方式,请在服务器配置之后重新访问");
        }
        String code = servcode + "@" + version;
        // 得到访问的地址
        String url = "http://localhost:" + serverPort + "/DCSBWeb/" + type + code;
        String responseMsg = null;
        String atomCode = esbInboundMainResult.get(0).getAtomcode();
        List<EsbAtomservice> esbAtomserviceResult = esbAtomServiceService.selectAtomServiceByCode(atomCode);
        if(esbAtomserviceResult.size() == 0){
            return Response.error("此接口:"+ atomCode +") 没有与之对应的原子服务，请在服务器配置之后重新访问");
        }
        String contentType = esbAtomserviceResult.get(0).getContentType();
        String httpType = "";
        if(!isEmpty(esbInboundHttpResult)) {
            httpType = String.valueOf(esbInboundHttpResult.getHttptype());
        }else if(!isEmpty(esbInboundWebserviceResult)) {

        }
        if("servlets/".equals(type)) {
            if("1".equals(httpType)) {
                StringBuffer sb = new StringBuffer();
                for (Map.Entry<String, Object> entry : object.entrySet()) {
                    sb.append(entry.getKey()+ "=" + object.get(entry.getKey()) + "&");
                }
                if(sb.toString().contains("&")) {
                    url = url + "?" + sb.toString().substring(0, sb.toString().lastIndexOf("&"));
                }else {

                }
                responseMsg = HttpReqUtil.get(url, "UTF-8", 60000, headerMap);
            }else if("2".equals(httpType)) {
                if ("1".equals(contentType)) {
                    contentType = "multipart/form-data";
                }else if ("2".equals(contentType)) {
                    contentType = "application/json";
                }else if ("3".equals(contentType)) {
                    contentType = "application/x-www-form-urlencoded";
                }else if ("4".equals(contentType)) {
                    contentType = "text/xml";
                }else if ("5".equals(contentType)) {
                    contentType = "application/xml";
                }else {
                    contentType = "multipart/form-data";
                }
                if("multipart/form-data".equals(contentType)) {
                    Part[] parts = new Part[object.size()];
                    int i = 0;
                    for (Map.Entry<String, Object> entry : object.entrySet()) {
                        parts[i] = new StringPart(entry.getKey(), String.valueOf(object.get(entry.getKey())), "UTF-8");
                        i++;
                    }
                    responseMsg = HttpReqUtil.postMultipart(url, parts, "UTF-8", "UTF-8", 60000, headerMap);
                }else if ("application/json".equals(contentType)) {
                    try {
                        responseMsg = HttpReqUtil.postJson(url, object.toJSONString(), "UTF-8", "UTF-8", 60000, headerMap);
                    } catch (Exception e) {
                        responseMsg = "Request error. " + e.getMessage();
                        return Response.error(responseMsg);
                    }
                }else if ("application/x-www-form-urlencoded".equals(contentType)) {
                    NameValuePair[] param = new NameValuePair[object.size()];
                    int i = 0;
                    for (Map.Entry<String, Object> entry : object.entrySet()) {
                        param[i] = new NameValuePair(entry.getKey(), String.valueOf(object.get(entry.getKey())));
                        i++;
                    }
                    responseMsg = HttpReqUtil.postUrlencoded(url, param, "UTF-8", "UTF-8", 60000, headerMap);
                }
            }
        }else if("services/".equals(type)) {
            return Response.error("webservice接口暂不支持服务模拟");
        }
        return Response.success(responseMsg);
    }

    @GetMapping("/txtExport")
    @ResponseBody
    @ApiOperation("标准服务导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="服务serviceid数组", required=true, paramType="query" ,allowMultiple=true, dataType = "Integer"),
    })
    public Response txtExport(Integer[] ids, HttpServletResponse response, HttpServletRequest request) {
        String fileName = "";
        StringBuffer content = new StringBuffer();
        if (ids.length == 0) {
            return Response.error("请选择需要导出的服务");
        }
        List<EsbInboundMainAndAtomservice> esbInboundMainAndAtomservices = esbInboundMainService.selectBanch(ids);
        String str = "";
        QueryWrapper<EsbInboundMain> esbInboundMainQueryWrapper = new QueryWrapper<>();
        esbInboundMainQueryWrapper.in("serviceid", ids);
        List<EsbInboundMain> esbInboundMains = esbInboundMainService.list(esbInboundMainQueryWrapper);
        for (EsbInboundMain esbInboundMain : esbInboundMains) {
            if (isEmpty(esbInboundMain.getAtomcode())) {
                str = esbInboundMain.getServname();
                return Response.error("没有与之对应的原子服务编码，此条数据无法导出，服务名称为：" + str);
            }
        }

        for (int i = 0; i < esbInboundMainAndAtomservices.size(); i++) {

            fileName = "标准服务导出";
            JSONObject jsonMain = new JSONObject();
            JSONArray jsonArray = new JSONArray();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date mainCreateTime = esbInboundMainAndAtomservices.get(i).getCreateTime();
            String inboundMainCreateTime = dateFormat.format(mainCreateTime);
            Date mainUpdateTime = esbInboundMainAndAtomservices.get(i).getUpdateTime();
            if (!isEmpty(mainUpdateTime)) {
                String inboundMainUpdateTime = dateFormat.format(mainUpdateTime);
                jsonMain.put("updateTime", inboundMainUpdateTime);
                jsonMain.put("serviceid", esbInboundMainAndAtomservices.get(i).getServiceid());
                jsonMain.put("servname", esbInboundMainAndAtomservices.get(i).getServname());
                jsonMain.put("servcode", esbInboundMainAndAtomservices.get(i).getServcode());
                jsonMain.put("servversion", esbInboundMainAndAtomservices.get(i).getServversion());
                jsonMain.put("status", esbInboundMainAndAtomservices.get(i).getStatus());
                jsonMain.put("atomcode", esbInboundMainAndAtomservices.get(i).getAtomcode());
                jsonMain.put("overTime", esbInboundMainAndAtomservices.get(i).getOverTime());
                jsonMain.put("createTime", inboundMainCreateTime);
            }
            jsonMain.put("serviceid", esbInboundMainAndAtomservices.get(i).getServiceid());
            jsonMain.put("servname", esbInboundMainAndAtomservices.get(i).getServname());
            jsonMain.put("servcode", esbInboundMainAndAtomservices.get(i).getServcode());
            jsonMain.put("servversion", esbInboundMainAndAtomservices.get(i).getServversion());
            jsonMain.put("status", esbInboundMainAndAtomservices.get(i).getStatus());
            jsonMain.put("atomcode", esbInboundMainAndAtomservices.get(i).getAtomcode());
            jsonMain.put("overTime", esbInboundMainAndAtomservices.get(i).getOverTime());
            jsonMain.put("createTime", inboundMainCreateTime);
            jsonMain.put("updateTime", " ");
            jsonArray.add(jsonMain);

            JSONObject jsonAtom = new JSONObject();
            Date atomCreateTime = esbInboundMainAndAtomservices.get(i).getAtomCreateTime();
            String atomServiceCreateTime = dateFormat.format(atomCreateTime);
            Date atomUpdateTime = esbInboundMainAndAtomservices.get(i).getAtomUpdateTime();
            if (!isEmpty(atomUpdateTime)) {
                String atomServiceUpdateTime = dateFormat.format(atomUpdateTime);
                jsonAtom.put("id", esbInboundMainAndAtomservices.get(i).getId());
                jsonAtom.put("systemId", esbInboundMainAndAtomservices.get(i).getSystemId());
                jsonAtom.put("atomserviceName", esbInboundMainAndAtomservices.get(i).getAtomserviceName());
                jsonAtom.put("atomserviceCode", esbInboundMainAndAtomservices.get(i).getAtomserviceCode());
                jsonAtom.put("adapterType", esbInboundMainAndAtomservices.get(i).getAdapterType());
                jsonAtom.put("contentType", esbInboundMainAndAtomservices.get(i).getContentType());
                jsonAtom.put("maxClients", esbInboundMainAndAtomservices.get(i).getMaxClients());
                jsonAtom.put("atomserviceVersion", esbInboundMainAndAtomservices.get(i).getAtomserviceVersion());
                jsonAtom.put("atomStatus", esbInboundMainAndAtomservices.get(i).getAtomStatus());
                jsonAtom.put("atomOverTime", esbInboundMainAndAtomservices.get(i).getAtomOverTime());
                jsonAtom.put("atomCreateTime", atomServiceCreateTime);
                jsonAtom.put("atomUpdateTime", atomServiceUpdateTime);
            }
            jsonAtom.put("id", esbInboundMainAndAtomservices.get(i).getId());
            jsonAtom.put("systemId", esbInboundMainAndAtomservices.get(i).getSystemId());
            jsonAtom.put("atomserviceName", esbInboundMainAndAtomservices.get(i).getAtomserviceName());
            jsonAtom.put("atomserviceCode", esbInboundMainAndAtomservices.get(i).getAtomserviceCode());
            jsonAtom.put("adapterType", esbInboundMainAndAtomservices.get(i).getAdapterType());
            jsonAtom.put("contentType", esbInboundMainAndAtomservices.get(i).getContentType());
            jsonAtom.put("maxClients", esbInboundMainAndAtomservices.get(i).getMaxClients());
            jsonAtom.put("atomserviceVersion", esbInboundMainAndAtomservices.get(i).getAtomserviceVersion());
            jsonAtom.put("atomStatus", esbInboundMainAndAtomservices.get(i).getAtomStatus());
            jsonAtom.put("atomOverTime", esbInboundMainAndAtomservices.get(i).getAtomOverTime());
            jsonAtom.put("atomCreateTime", atomServiceCreateTime);
            jsonAtom.put("atomUpdateTime", " ");
            jsonArray.add(jsonAtom);

            JSONObject jsonMainHttp = new JSONObject();
            jsonMainHttp.put("inboundHttpServiceid", esbInboundMainAndAtomservices.get(i).getInboundHttpServiceid());
            jsonMainHttp.put("inboundHttpHttptype", esbInboundMainAndAtomservices.get(i).getInboundHttpHttptype());
            jsonArray.add(jsonMainHttp);

            JSONObject jsonMainWebService = new JSONObject();
            jsonMainWebService.put("inboundWebServiceServiceid", esbInboundMainAndAtomservices.get(i).getInboundWebServiceServiceid());
            jsonMainWebService.put("endpoint", esbInboundMainAndAtomservices.get(i).getEndpoint());
            jsonMainWebService.put("inboundWebServiceServicename", esbInboundMainAndAtomservices.get(i).getInboundWebServiceServicename());
            jsonMainWebService.put("portname", esbInboundMainAndAtomservices.get(i).getPortname());
            jsonMainWebService.put("operationname", esbInboundMainAndAtomservices.get(i).getOperationname());
            jsonArray.add(jsonMainWebService);

            JSONObject jsonAtomHttp = new JSONObject();
            jsonAtomHttp.put("adapterHttpId", esbInboundMainAndAtomservices.get(i).getAdapterHttpId());
            jsonAtomHttp.put("serviceUrl", esbInboundMainAndAtomservices.get(i).getServiceUrl());
            jsonAtomHttp.put("scharset", esbInboundMainAndAtomservices.get(i).getScharset());
            jsonAtomHttp.put("rcharset", esbInboundMainAndAtomservices.get(i).getRcharset());
            jsonAtomHttp.put("httptype", esbInboundMainAndAtomservices.get(i).getHttptype());
            jsonArray.add(jsonAtomHttp);

            JSONObject jsonAtomSoap = new JSONObject();
            jsonAtomSoap.put("soapId", esbInboundMainAndAtomservices.get(i).getSoapId());
            jsonAtomSoap.put("soapServiceUrl", esbInboundMainAndAtomservices.get(i).getSoapServiceUrl());
            jsonAtomSoap.put("soapScharset", esbInboundMainAndAtomservices.get(i).getSoapScharset());
            jsonAtomSoap.put("soapRcharset", esbInboundMainAndAtomservices.get(i).getSoapRcharset());
            jsonArray.add(jsonAtomSoap);

            content = content.append(jsonArray)
                    .append("\r\n");
        }
        /**
         * 拼接txt
         */
        StringBuffer txtContent = new StringBuffer();
        txtContent = txtContent.append(content);
        /**
         * 写入txt文件
         */
        File outfile = new File(request.getServletContext().getRealPath("/") + fileName + ".txt");
        File file = new File(outfile + ".txt");

        try {
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println(txtContent);

            OutputStream ops = response.getOutputStream();
            InputStream ins = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int bytesRead = 0;
            response.setContentType("text/html;charset=utf-8");
            boolean isMSIE = SearchBrowserKeywordsUtil.isMSBrowser(request);
            if (isMSIE) {
                /**
                 * IE、360兼容版浏览器的乱码问题解决
                 */
                fileName = URLEncoder.encode(fileName, "UTF-8");
            } else {
                /**
                 *万能乱码问题解决
                 */
                fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            }
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".txt");

            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                ops.write(buffer, 0, bytesRead);
            }
            ops.close();
            ins.close();
        } catch (Exception e) {

            return Response.error("文件导出失败:" + e.getMessage());
        }
        try {
            userOperationLogService.savaUserLog(request, "导入导出之导出");
        } catch (Exception e) {

        }
        return Response.success(file);
    }

    /**
     * 标准服务导入
     * @param file 文件
     * @return
     */
    @PostMapping("/importData")
    @ResponseBody
    @ApiOperation(value = "标准服务导入")
    public Response importData(HttpServletRequest request, @RequestPart("file") MultipartFile file) {
        if(!file.getOriginalFilename().contains(".txt")) {
            return Response.error("上传的文件类型不对");
        }
        InputStreamReader isR = null;
        try{
            InputStream inputStream = file.getInputStream();
            isR = new InputStreamReader(inputStream, "UTF-8");
        }catch (Exception e) {
            return Response.error("文件读取失败");
        }
        BufferedReader br = new BufferedReader(isR);
        String line = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strMess = "";
        try {
            while ((line = br.readLine()) != null) {
                JSONArray dataArray = new JSONArray();
                if (!isEmpty(line)) {
                    dataArray = JSONArray.parseArray(line);

                    String a = String.valueOf(dataArray.get(0)) ;
                    JSONObject esbInboundMainObject = JSONObject.parseObject(a);
                    Integer serviceid = (Integer)esbInboundMainObject.get("serviceid");
                    String servname = (String) esbInboundMainObject.get("servname");
                    String servcode = (String)esbInboundMainObject.get("servcode");
                    String servversion = (String)esbInboundMainObject.get("servversion");
                    String status = (String)esbInboundMainObject.get("status");
                    String atomcode = (String)esbInboundMainObject.get("atomcode");
                    String overTime = (String)esbInboundMainObject.get("overTime");
                    String createTime =(String) esbInboundMainObject.get("createTime");
                    Date inboundMaincreateTime = dateFormat.parse(createTime) ;

                    EsbInboundMain esbInboundMain = new EsbInboundMain();

                    if (!esbInboundMainObject.get("updateTime").equals(" ")){
                        String updateTime =(String) esbInboundMainObject.get("updateTime");
                        Date inboundMaincreateUpdateTime = dateFormat.parse(updateTime);
                        esbInboundMain.setUpdateTime(inboundMaincreateUpdateTime);
                    }
                    esbInboundMain.setServiceid(serviceid);
                    esbInboundMain.setServname(servname);
                    esbInboundMain.setServcode(servcode);
                    esbInboundMain.setServversion(servversion);
                    esbInboundMain.setStatus(status);
                    esbInboundMain.setAtomcode(atomcode);
                    esbInboundMain.setOverTime(overTime);
                    esbInboundMain.setCreateTime(inboundMaincreateTime);
                    esbInboundMain.setUpdateTime(null);

                    QueryWrapper<EsbInboundMain> wrapper = new QueryWrapper<>();
                    wrapper.eq("servcode", servcode);
                    EsbInboundMain esbInboundMainOne = esbInboundMainService.getOne(wrapper);
                    if (!isEmpty(esbInboundMainOne)){
                        if (!isEmpty(strMess)){
                            strMess = strMess +  "," + esbInboundMainOne.getServcode();
                        }else {
                            strMess = strMess + esbInboundMainOne.getServcode();
                        }
                        continue;
                    }

                    try{
                        boolean esbMainSave = esbInboundMainService.save(esbInboundMain);
                        if (esbMainSave){
                            EsbServiceTree esbServiceTree = new EsbServiceTree();
                            esbServiceTree.setCode(esbInboundMain.getServiceid());
                            esbServiceTree.setLvl(2);
                            esbServiceTree.setParId(1);
                            esbServiceTree.setName(servname);
                            esbServiceTree.setHaschildren(0);
                            esbServiceTree.setStatus("1");
                            esbServiceTreeService.save(esbServiceTree);
                        }
                    }catch (Exception esbInboundMainE) {

                    }

                    String b = String.valueOf(dataArray.get(1)) ;
                    JSONObject esbAtomServiceObject = JSONObject.parseObject(b);
                    Integer id = (Integer) esbAtomServiceObject.get("id");
                    Integer systemId = (Integer) esbAtomServiceObject.get("systemId");
                    String atomserviceName = (String) esbAtomServiceObject.get("atomserviceName");
                    String atomserviceCode = (String) esbAtomServiceObject.get("atomserviceCode");
                    String adapterType = (String) esbAtomServiceObject.get("adapterType");
                    String contentType = (String) esbAtomServiceObject.get("contentType");
                    String maxClients = (String) esbAtomServiceObject.get("maxClients");
                    String atomserviceVersion = (String) esbAtomServiceObject.get("atomserviceVersion");
                    String atomStatus = (String) esbAtomServiceObject.get("atomStatus");
                    String atomOverTime = (String) esbAtomServiceObject.get("atomOverTime");
                    String atomCreateTime = (String) esbAtomServiceObject.get("atomCreateTime");
                    Date atomServiceCreateTime = dateFormat.parse(atomCreateTime);
                    EsbAtomservice esbAtomservice =  new EsbAtomservice();
                    if (!esbAtomServiceObject.get("atomUpdateTime").equals(" ")){
                        String atomUpdateTime = (String) esbAtomServiceObject.get("atomUpdateTime");
                        Date atomServiceUpdateTime = dateFormat.parse(atomUpdateTime);
                        esbAtomservice.setUpdateTime(atomServiceUpdateTime);
                    }
                    esbAtomservice.setId(id);
                    esbAtomservice.setSystemId(systemId);
                    esbAtomservice.setAtomserviceName(atomserviceName);
                    esbAtomservice.setAtomserviceCode(atomserviceCode);
                    esbAtomservice.setAdapterType(adapterType);
                    esbAtomservice.setContentType(contentType);
                    esbAtomservice.setMaxClients(maxClients);
                    esbAtomservice.setAtomserviceVersion(atomserviceVersion);
                    esbAtomservice.setStatus(atomStatus);
                    esbAtomservice.setOverTime(atomOverTime);
                    esbAtomservice.setCreateTime(atomServiceCreateTime);
                    esbAtomservice.setUpdateTime(null);
                    try{
                        esbAtomServiceService.save(esbAtomservice);
                    }catch (Exception esbAtomserviceE) {

                    }

                    JSONObject esbInboundMainHttpObject = new JSONObject();
                    String c = String.valueOf(dataArray.get(2)) ;
                    if (! "{}".equals(c)){
                        esbInboundMainHttpObject = JSONObject.parseObject(c);
                        Integer inboundHttpHttptype = (Integer)esbInboundMainHttpObject.get("inboundHttpHttptype");
                        EsbInboundHttp esbInboundHttp = new EsbInboundHttp();
                        esbInboundHttp.setServiceid(esbInboundMain.getServiceid());
                        esbInboundHttp.setHttptype(inboundHttpHttptype);
                        try{
                            esbInboundHttpService.save(esbInboundHttp);
                        }catch (Exception esbInboundHttpE) {

                        }
                    }

                    JSONObject esbInboundMainWebServiceObject = new JSONObject();
                    String d = String.valueOf(dataArray.get(3)) ;
                    if (! "{}".equals(d)){
                        esbInboundMainWebServiceObject = JSONObject.parseObject(d);
                        String endpoint = (String)esbInboundMainWebServiceObject.get("endpoint");
                        String inboundWebServiceServicename = (String)esbInboundMainWebServiceObject.get("inboundWebServiceServicename");
                        String portname = (String)esbInboundMainWebServiceObject.get("portname");
                        String operationname = (String)esbInboundMainWebServiceObject.get("operationname");
                        EsbInboundWebservice esbInboundWebservice = new EsbInboundWebservice();
                        esbInboundWebservice.setServiceid(esbInboundMain.getServiceid());
                        esbInboundWebservice.setEndpoint(endpoint);
                        esbInboundWebservice.setServicename(inboundWebServiceServicename);
                        esbInboundWebservice.setPortname(portname);
                        esbInboundWebservice.setOperationname(operationname);
                        try{
                            esbInboundWebserviceService.save(esbInboundWebservice);
                        }catch (Exception esbInboundWebserviceE) {

                        }
                    }

                    JSONObject esbAtomServiceHttpObject = new JSONObject();
                    String e = String.valueOf(dataArray.get(4)) ;
                    if (! "{}".equals(e)){
                        esbAtomServiceHttpObject = JSONObject.parseObject(e);
                        String serviceUrl = (String)esbAtomServiceHttpObject.get("serviceUrl");
                        String scharset = (String)esbAtomServiceHttpObject.get("scharset");
                        String rcharset = (String)esbAtomServiceHttpObject.get("rcharset");
                        String httptype = (String)esbAtomServiceHttpObject.get("httptype");
                        EsbAdapderHttp esbAdapderHttp = new EsbAdapderHttp();
                        esbAdapderHttp.setId(esbAtomservice.getId());
                        esbAdapderHttp.setServiceUrl(serviceUrl);
                        esbAdapderHttp.setScharset(scharset);
                        esbAdapderHttp.setRcharset(rcharset);
                        esbAdapderHttp.setHttptype(httptype);
                        try{
                            esbAdapderHttpService.save(esbAdapderHttp);
                        }catch (Exception esbAdapderHttpE) {

                        }
                    }

                    JSONObject esbAtomServiceSoapObject = new JSONObject();
                    String f = String.valueOf(dataArray.get(5)) ;
                    if (! "{}".equals(f)){
                        esbAtomServiceSoapObject = JSONObject.parseObject(f);
                        String soapServiceUrl = (String) esbAtomServiceSoapObject.get("soapServiceUrl");
                        String soapScharset = (String) esbAtomServiceSoapObject.get("soapScharset");
                        String soapRcharset = (String) esbAtomServiceSoapObject.get("soapRcharset");
                        EsbAdapderSoap esbAdapderSoap = new EsbAdapderSoap();
                        esbAdapderSoap.setId(esbAtomservice.getId());
                        esbAdapderSoap.setServiceUrl(soapServiceUrl);
                        esbAdapderSoap.setScharset(soapScharset);
                        esbAdapderSoap.setRcharset(soapRcharset);
                        try{
                            esbAdapderSoapService.save(esbAdapderSoap);
                        }catch (Exception esbAdapderSoapE) {

                        }
                    }
                }
            }
        }catch (IOException e) {
            log.info("IO异常：" + e.getMessage());
        }catch (Exception e) {
            log.info("转换异常：" + e.getMessage());
        }finally{
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {

                }
            }
        }
        if (!isEmpty(strMess)){
            return Response.success("部分数据导入成功。导入的文件中存在重复的服务数据，这些数据无法导入，服务编码分别为:" + strMess);
        }
        try{
            userOperationLogService.savaUserLog(request, "导入导出之导入");
        }catch (Exception e) {

        }
        return Response.success("导入成功");
    }

}
